package com.dw.monitor.tool;



import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestUtil {

    //get
    public static void getMethod(String url) throws IOException {
        URL restURL = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

        conn.setRequestMethod("GET"); // POST GET PUT DELETE
        conn.setRequestProperty("Accept", "application/json");

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while((line = br.readLine()) != null ){
            JsonObject json = (JsonObject)new JsonParser().parse(line);
            //JsonArray data = json.getAsJsonArray("app");
            JsonElement value = json.get("apps").getAsJsonObject().get("app");
            JsonArray jsonElements = value.getAsJsonArray();
            if(jsonElements.size()>0){
                int sum = 0;
                for (int i = 0;i< jsonElements.size();i++){
                    JsonObject data = (JsonObject) jsonElements.get(i);
                    String id = data.get("id").toString();
                    String user = data.get("user").toString();
                    String queue = data.get("queue").toString();
                    String state = data.get("state").toString();
                    String finalStatus = data.get("finalStatus").toString();
                    String applicationType = data.get("applicationType").toString();
                    String startedTime = data.get("startedTime").toString();
                    String finishedTime = data.get("finishedTime").toString();
                    String elapsedTime = data.get("elapsedTime").toString();
                    String runningContainers = data.get("runningContainers").toString();
                    String allocatedVCores = data.get("allocatedVCores").toString();
                    System.out.print("id:"+id);
                    System.out.print(",user:"+user);
                    System.out.print(",queue:"+queue);
                    System.out.print(",state:"+state);
                    System.out.print(",finalStatus:"+finalStatus);
                    System.out.print(",applicationType:"+applicationType);
                    System.out.print(",startedTime:"+startedTime);
                    System.out.print(",finishedTime:"+finishedTime);
                    System.out.print(",elapsedTime:"+elapsedTime);
                    System.out.print(",runningContainers:"+runningContainers);
                    System.out.print(",allocatedVCores:"+allocatedVCores);

                    //System.out.println(jsonElements.get(i));
                    System.out.println();
                    sum = sum+1;
                }
                System.out.println(sum);
            }
            //JsonArray jsonElements = (JsonObject)new JsonParser().parse(value).getAsJsonArray();
            //String arr = value.toString();
            //String json = JSONObject.toJSONString(line);
            //JSONObject value = JSONObject.parseObject(json);
            //String value_1 = value.getString("apps");
            //JSONObject data = JSONObject.parseObject(value_1).getJSONArray("app").getJSONObject(0);
            //String value_2 = json1.getString("app");
            System.out.println(jsonElements);
            //System.out.println("开始"+data);
        }

        br.close();
    }

    public static void getMethod1(String url) throws IOException {
        URL restURL = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();

        conn.setRequestMethod("GET"); // POST GET PUT DELETE
        conn.setRequestProperty("Accept", "application/json");

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while((line = br.readLine()) != null ){
            System.out.println(line);
        }

        br.close();
    }

    //post
    public static void postMethod(String url, String query) throws IOException {
        URL restURL = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);
        ps.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while((line = br.readLine()) != null ){
            System.out.println(line);
        }

        br.close();
    }

    public static void main(String[] args) {
        RestUtil restUtil = new RestUtil();
        try {
            //restUtil.getMethod("http://172.16.2.162:8088/ws/v1/cluster/apps");
            restUtil.getMethod1("http://172.16.2.162:8088/ws/v1/cluster/metrics");
            //String url = "http://172.16.2.162:8088/ws/v1/cluster/apps";
            //String query = ""; //json格式
           // restUtil.postMethod(url, query);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

}
